package com.mediacloud.app.newsmodule.activity

import android.content.Context
import android.graphics.Color
import androidx.core.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.mediacloud.app.model.news.LiveProgramListReciver
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import com.mediacloud.app.newsmodule.interfaces.DataInitCall
import java.text.SimpleDateFormat
import java.util.*

class ChatLiveProgramDateAdapter(context: Context?) : BaseRecyclerAdapterX<LiveProgramListReciver.ProgramWeekItem>(context) {
    var call: DataInitCall? = null
    var selectedIndex: Int =0
        set(value) {
            field = value
            notifyDataSetChanged()
        }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ProgramDateHolder(LayoutInflater.from(getContext()).inflate(R.layout.navprogram_dateitem_chat, parent, false))
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is ProgramDateHolder) {
            val item = getItem(position)
            holder.update(position == selectedIndex, position, item)
        }
    }

    fun setSelectedDay(day: String) {
        for (i in 0 until data.size) {
            if (data[i].date == day) {
                selectedIndex = i
                break
            }
        }
        call?.onInitSelect(selectedIndex)
        notifyDataSetChanged()
    }

    inner class ProgramDateHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        val navDayTxt: TextView = itemView.findViewById(R.id.navDayTxt)
        val navDateTxt: TextView = itemView.findViewById(R.id.navDateTxt)
        var today: String? = null

        init {

            val sdf = SimpleDateFormat("yyyyMMdd", Locale.CHINA)
            today = sdf.format(System.currentTimeMillis())
        }

        fun update(isSelected: Boolean, position: Int, dateItem: LiveProgramListReciver.ProgramWeekItem) {
            if (!isSelected)
                itemView.setBackgroundColor(Color.TRANSPARENT)
//                itemView.setBackgroundColor(ContextCompat.getColor(itemView.context,R.color.appfacprogramdatebg))
            else
                itemView.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.navprogam_dateselected_1))
            navDayTxt.text = getWeek(dateItem.week)



            if (dateItem.date == today) {
                navDateTxt.text = "今日"
            } else {
                navDateTxt.text = getDate(dateItem.title)
            }
        }

        private fun getDate(date: String): String {
            val datas = date.split("月".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            if (datas.size > 1) {
                val datass = datas[1].split("日".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                return if (datass.size > 0) {
                    datass[0]
                } else {
                    date
                }
            }
            return date
        }

        private fun getWeek(week: String): String {
            return if ("0" == week) {
                "周日"
            } else if ("1" == week) {
                "周一"
            } else if ("2" == week) {
                "周二"
            } else if ("3" == week) {
                "周三"
            } else if ("4" == week) {
                "周四"
            } else if ("5" == week) {
                "周五"
            } else {
                "周六"
            }
        }


    }


}